﻿using streamsearcher;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

class Listing_01
{

    internal class Program
    {
        static readonly int buffer_size = 1024*10;
        private static void Main(string[] args)
        {
            //var patternString = @"3 ? 绥\;?; 憹j辯! ?";
            //var patternBytes = Encoding.ASCII.GetBytes(patternString);

            var patternBytes = new byte[]  {
                0xE6, 0x0A, 0x27, 0xA0, 0x14, 0x82, 0x91, 0x23, 0x24, 0x01, 0x63, 0x0B, 0x4D, 0x44, 0xD6, 0x13
};
            var replaceBytes = new byte[]  {
                0xB4, 0x04, 0xA2, 0x56, 0x16, 0xA6, 0x8C, 0x5D, 0xB7, 0x4D, 0xEA, 0xE8, 0xC1, 0xB3, 0x7A, 0x7A
            };

            //patternBytes = replaceBytes;
            var searcher = new StreamSearcher(patternBytes); //创建搜索器

            foreach (var fname in new[] { "1.jpg" })
            {
                using (var stream = File.Open(fname, FileMode.Open, FileAccess.ReadWrite))
                {
                    long absuluted_start; //用于记录匹配帧在流的起始位置
                    long absuluted_end; //用于记录匹配帧在流的末尾位置
                    absuluted_start = absuluted_end = stream.Position; //开始都指向文件流的起始位置
                    
                    using (var sr = new BinaryReader(stream))                    
                    {
                        while (sr.BaseStream.Position + patternBytes.Length < sr.BaseStream.Length ) //是否读到最后一帧
                        {
                            try
                            {
                                byte[] data = sr.ReadBytes(buffer_size); //读一帧
                                long _start;
                                long _end;
                                var ms = new MemoryStream(data); //创建内存流
                                while (ms.Position < ms.Length) //没有读到内存流末尾
                                {
                                    var position = searcher.Search(ms); //搜索在内存流中匹配的位置
                                    if (position == -1) //找不到匹配位置，不再搜索
                                    {
                                        stream.Seek(stream.Position - (patternBytes.Length - 1), SeekOrigin.Begin);  //回退到前一帧不超过模式串长度位置                                       
                                        break; 
                                    }
                                    else //找到
                                    {

                                        absuluted_end = stream.Position - (ms.Length - ms.Position); //确定该帧在原文件流对应绝对位置
                                        Console.WriteLine($"New Frame-absoluted start:{absuluted_start},absoluted end:{absuluted_end}"); //这里只做了简单输出，可以做原始流截取帧处理
                                        absuluted_start = absuluted_end + 1; //下一帧的开始位置

                                    }
                                }
                            }
                            catch (EndOfStreamException ex)

                            {
                                Console.WriteLine("Done!");
                                break;
                            }
                        }
                    }
                    
                } 
            }
        }

    }

}